﻿//https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root)
    {
        vector<vector<int>> ret;
        queue<TreeNode*> q;
        if (root == nullptr)
            return ret;
        q.push(root);

        int level = 1;
        while (q.size())
        {
            int sz = q.size();
            vector<int> temp;
            for (int i = 0; i < sz; i++)
            {
                TreeNode* r = q.front();
                temp.push_back(r->val);
                q.pop();

                if (r->left) q.push(r->left);
                if (r->right) q.push(r->right);
            }
            if (level % 2 == 0) reverse(temp.begin(), temp.end());
            level++;
            ret.push_back(temp);
        }
        return ret;
    }
};